<?php
if ($_POST) {
    header("Content-type:text/html;charset=utf-8");
    include '../../api/config.php';
    //数据库配置文件
    $json = $_POST["data"];
    $data = json_decode($json);
    $id = $data->head;
    if ($id == '1050') {
        $token = $data->token;
        $page = $data->page;
        $page = ($page - 1) * 10;
        $sql = "SELECT * FROM admin_user WHERE token='{$token}'";
        $sql1 = "SELECT * FROM subscribe WHERE flg=1 ORDER BY id DESC LIMIT {$page},10";
        $sql2 = "SELECT count(*) FROM subscribe WHERE flg=1";
    }
    if ($id == '1051') {
        $token = $data->token;
        $ids = $data->id;
        $flg = $data->flg;
        $sql = "SELECT * FROM admin_user WHERE token='{$token}'";
        $sql3 = "DELETE FROM subscribe WHERE id = '{$ids}'";
    }
    if ($id == '1062') {
        $token = $data->token;
        $key = $data->keys;
        $flg = $data->flg;
        $page = $data->page;
        $page = ($page - 1) * 10;
        $sql = "SELECT * FROM admin_user WHERE token='{$token}'";
        $sql2 = "SELECT count(*) FROM subscribe WHERE flg='{$flg}'";
        $sql9 = "SELECT * FROM subscribe WHERE (email LIKE '%{$key}%' OR id LIKE '%{$key}%') AND flg='{$flg}';";
    }

    // 查找用户
    $arr = array();
    $arr['data'] = array();
    $stmt = $conn->query($sql);
    $stmt->execute();
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
    foreach (new RecursiveArrayIterator($stmt->fetchAll()) as $k => $v) {
        array_push($arr['data'], $v);
        //添加到数组
    }

    //加入回收站
    if ($id == '1051') {
        if ($arr['data']) {
            $arr2 = array();
            $stmt3 = $conn->prepare($sql3);
            $stmt3->execute();
            $result3 = $stmt3->setFetchMode(PDO::FETCH_ASSOC);
            if ($result3) {
                $arr1['code'] = '200';
                $arr1['msg'] = '操作成功~';
                $data = json_encode($arr1);
                //把数据转换为JSON数据.
                echo $data;
            } else {
                $arr1['code'] = '400';
                $arr1['msg'] = '数据异常~';
                $data = json_encode($arr1);
                //把数据转换为JSON数据.
                echo $data;
            }
        } else {
            $arr['code'] = '500';
            $arr['msg'] = '权限不足';
            $arr['href'] = '../index.html';
            $data = json_encode($arr);
            //把数据转换为JSON数据.
            echo $data;
        }
    }

    // 搜索
    if ($id == '1062') {
        $arr1 = array();
        $arr1['data'] = array();
        $stmt1 = $conn->query($sql9);
        $stmt1->execute();
        $result1 = $stmt1->setFetchMode(PDO::FETCH_ASSOC);
        foreach (new RecursiveArrayIterator($stmt1->fetchAll()) as $k1 => $v1) {
            array_push($arr1['data'], $v1);
            //添加到数组
        }
        $stmt2 = $conn->query($sql2);
        $stmt2->execute();
        $result2 = $stmt2->setFetchMode(PDO::FETCH_ASSOC);
        $all_page = $stmt2->fetchAll()[0]["count(*)"];
        if ($arr['data']) {
            if ($result1) {
                $arr1['code'] = '200';
                $arr1['all_page'] = $all_page / 10;
                $arr1['msg'] = '获取数据成功~';
                $data = json_encode($arr1);
                //把数据转换为JSON数据.
                echo $data;
            } else {
                $arr1['code'] = '400';
                $arr1['msg'] = '数据异常~';
                $data = json_encode($arr1);
                //把数据转换为JSON数据.
                echo $data;
            }
        } else {
            $arr['code'] = '500';
            $arr['msg'] = '权限不足';
            $arr['href'] = '../index.html';
            $data = json_encode($arr);
            //把数据转换为JSON数据.
            echo $data;
        }
    }

    if ($id == '1050') {
        $arr1 = array();
        $arr1['data'] = array();
        $stmt1 = $conn->query($sql1);
        $stmt1->execute();
        $result1 = $stmt1->setFetchMode(PDO::FETCH_ASSOC);
        foreach (new RecursiveArrayIterator($stmt1->fetchAll()) as $k1 => $v1) {
            array_push($arr1['data'], $v1);
            //添加到数组
        }
        $stmt2 = $conn->query($sql2);
        $stmt2->execute();
        $result2 = $stmt2->setFetchMode(PDO::FETCH_ASSOC);
        $all_page = $stmt2->fetchAll()[0]["count(*)"];
        if ($arr['data']) {
            if ($result1) {
                $arr1['code'] = '200';
                $arr1['all_page'] = $all_page / 10;
                $arr1['msg'] = '获取数据成功~';
                $data = json_encode($arr1);
                //把数据转换为JSON数据.
                echo $data;
            } else {
                $arr1['code'] = '400';
                $arr1['msg'] = '数据异常~';
                $data = json_encode($arr1);
                //把数据转换为JSON数据.
                echo $data;
            }
        } else {
            $arr['code'] = '500';
            $arr['msg'] = '权限不足';
            $arr['href'] = '../index.html';
            $data = json_encode($arr);
            //把数据转换为JSON数据.
            echo $data;
        }
    }
    // 输出内容
    $conn = null;
} else {
    echo "<script>\r\n    window.location.href = '../../';\r\n    </script>";
}